package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired
    private IEmployeeService employeeService;
    @ResponseBody
    @RequestMapping("/login")
    public JsonResult login(String username , String password){
        try {
            Employee employee = employeeService.login(username, password);
            UserContext.setCurrentUser(employee);
            return new JsonResult();
        } catch(Exception e) {
            e.printStackTrace();
            //System.out.println(e.getMessage());
            return new JsonResult(false,e.getMessage());
        }
    }
    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/login.html";
    }
}
